package cn.wbu.zyh.mapper;

import cn.wbu.zyh.annotation.AutoFill;
import cn.wbu.zyh.dto.TeamMemberPageQueryDTO;
import cn.wbu.zyh.entity.Team;
import cn.wbu.zyh.entity.TeamMember;
import cn.wbu.zyh.enumeration.OperationType;
import cn.wbu.zyh.vo.TeamVO;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;

@Mapper
public interface TeamMapper {
    @Insert("insert into teams(id,name,description,created_User,created_time) VALUES (#{id},#{name},#{description},#{createdUser},#{createdTime})")
    void insert(Team team);

    @Insert("insert into team_members(team_id,username,role,created_time) VALUES (#{teamId},#{username},#{role},#{createdTime})")
    void joinTeam(TeamMember teamMember);
    @Update("UPDATE employee SET hasTeam = #{hasTeam} WHERE username = #{username}")
    void updateHasTeam(@Param("username") String username, @Param("hasTeam") int hasTeam);

    // 计算某个团队的成员数量
    @Select("SELECT COUNT(*) FROM team_members WHERE team_id = (SELECT id FROM teams WHERE created_user = #{username})")
    int countMembers(@Param("username") String username);

    // 删除团队记录
    @Delete("DELETE FROM teams WHERE created_user = #{username}")
    void deleteTeam(@Param("username") String username);

    @Delete("DELETE FROM teams WHERE id = #{teamId}")
    void deleteTeamWithId(@Param("teamId") String teamId);
    Page<TeamMember> pageQuery(TeamMemberPageQueryDTO teamMemberPageQueryDTO);
    TeamVO getHasTeam(@Param("username")String username);
    @Delete("DELETE FROM team_members WHERE username = #{username}")
    void deleteTeamMember(String username);
    @Update("UPDATE team_members SET role=#{role} WHERE username = #{username}")
    void updateTeamMemberRole(@Param("username") String username, @Param("role") int role);
    @Update("UPDATE teams SET created_user=#{newCreator} WHERE name = #{teamName}")
    void updateTeamCreatedUser(@Param("teamName") String teamName, @Param("newCreator")String newCreator);
}